﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Web;
using GP.Logic.Model;

namespace GP.Logic
{
    public class GpContext : DbContext
    {
        // webhotel10.iha.dk
        // Database: F11ITNET2Gr1
        // User: F11ITNET2DBUser
        // Pass: F11ITNET2DBUser
        // Data Source=webhotel10.iha.dk;Initial Catalog=F11ITNET2Gr1;User Id=F11ITNET2DBUser;Password=F11ITNET2DBUser;

        public DbSet<Doctor> Doctors { get; set; }
        public DbSet<Citizen> Citizens { get; set; }
        public DbSet<GrantedAccessTo> GrantedAccessTos { get; set; }
        public DbSet<GpSysUser> GpSysUsers { get; set; }
        public DbSet<GpSysRole> GpSysRoles { get; set; }
        public DbSet<Event> Events { get; set; }
        public DbSet<BloodPressure> BloodPressures { get; set; }
        public DbSet<Weight> Weights { get; set; }
        public DbSet<Oximeter> Oximeters { get; set; }
        public DbSet<ECG> Ecgs { get; set; }
        public DbSet<ECGValue> EcgValues { get; set; }
        public DbSet<Note> Notes { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            var events = modelBuilder.Entity<Event>();
            events.HasKey(a => a.EventId);
            events.ToTable("Event");
            events.Property(a => a.EventId).HasColumnName("EventId");
            
            // events.Property(a => a.)

            var doctor = modelBuilder.Entity<Doctor>();
            doctor.HasKey(a => a.DocId);
            doctor.ToTable("Doctor");

            var citizen = modelBuilder.Entity<Citizen>();
            citizen.HasKey(a => a.CitizenId);
            citizen.ToTable("Citizen");
            citizen.Property(a => a.CitizenId).HasColumnName("CitizenId");
            citizen.Property(a => a.DocId).HasColumnName("DocId");

            var gpSysUser = modelBuilder.Entity<GpSysUser>();
            gpSysUser.HasKey(a => a.UserId);
            gpSysUser.ToTable("GpSysUser");

            var granted = modelBuilder.Entity<GrantedAccessTo>();
            granted.HasKey(a => new {a.DocId, a.CitizenId});
            granted.ToTable("GrantedAccessTo");
            granted.Property(a => a.CitizenId).HasColumnName("CitizenId");
            granted.Property(a => a.DocId).HasColumnName("DocId");
            granted.Property(a => a.Expires).HasColumnName("Expires");

            var gpSysRole = modelBuilder.Entity<GpSysRole>();
            gpSysRole.HasKey(a => a.RoleId);
            gpSysRole.ToTable("GPSysRole");

            var bloodPressure = modelBuilder.Entity<BloodPressure>();
            bloodPressure.ToTable("BloodPressure");
            //bloodPressure.Property(a => a.EventId).HasColumnName("EventId");

            var oximeter = modelBuilder.Entity<Oximeter>();
            oximeter.ToTable("Oximeter");

            var weight = modelBuilder.Entity<Weight>();
            weight.ToTable("Weight");

            var vitalSign = modelBuilder.Entity<VitalSign>();
            vitalSign.ToTable("VitalSign");
            vitalSign.HasKey(a => a.VitalSignId);
            //vitalSign.Property(a => a.EventId).HasColumnName("EventId");
            //vitalSign.Map(a => a.Properties(b => b.));
            // vitalSign.Property(a => a.EventId).HasColumnName("EventId");
            
            var ecg = modelBuilder.Entity<ECG>();
            ecg.ToTable("ECGs");

            var ecgValue = modelBuilder.Entity<ECGValue>();
            ecgValue.HasKey(a => new {a.VitalSignId, a.ECGID});
            ecgValue.ToTable("ECGValues");

            var note = modelBuilder.Entity<Note>();
            note.HasKey(a => a.NoteId);
            note.ToTable("Note");

            // ecg.HasKey(a => a.VitalSignId);

            //var ecg = modelBuilder.Entity<ECG>();
            //ecg.HasKey(a => new {a.VitalSignId, a.ECGID});

            // var ecgs = modelBuilder.Entity<ECGs>();
            //bloodPressure.Map(a =>

            //                          a.MapInheritedProperties();
            //                          a.ToTable("VitalSign");
            //                      }).HasKey(a => a.VitalSignId);
            // bloodPressure.HasKey(a => a.VitalSignId);
        }
    }
}
